<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Cart_handler
{
	function __construct() {
		$this->CI = get_instance();
		$this->CI->load->library('cart');
	}
	
	public function get_rowid($id) {
		$rowid = $this->CI->cart->find_rowid_by_id($id);
		
		return $rowid;
	}

	public function get_item($rowid){
		return $this->CI->cart->get_item($rowid);
	}
	
	public function add_item($id = false, $quantity = false) {
		
		$prod = $this->CI->produkter->getProduct( $id );

		$data = array(
			'id'      => "prod_".$id,
			'qty'     => $quantity ? intval($quantity) : 0,
			'price'   => floatval($prod->is_offer ? $prod->offer_price : $prod->pris) * floatval($prod->paket),
			'name'    => $prod->namn." (".$prod->paket."kg)"
		);

		return $this->CI->cart->insert($data);
	}
	
	public function update_item($id, $quantity) {
		$data = array(
			'rowid' => $id,
			'qty' => $quantity ? $quantity : 0
		);
		return $this->CI->cart->update($data);
	}
	
	public function get_contents() {
		return $this->CI->cart->contents();
	}

	public function get_count(){
		return $this->CI->cart->total_items();
	}

	public function get_total(){
		return $this->CI->cart->total();
	}
	
	public function destroy_cart() {
		$this->CI->cart->destroy();
	}
}